import concurrent.futures
import threading
import time

# 定义线程要执行的函数
def worker(index):
    print(f"Thread {index} is starting")
    time.sleep(2)
    print(f"Thread {index} is done")

start = time.time()

# 使用线程池创建多线程
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    futures = [executor.submit(worker, i) for i in range(5)]
# 等待所有线程完成
concurrent.futures.wait(futures)

end = time.time()
print("concurrent cost: ", end - start, " seconds")

print("All threads are done")

